package com.huimingpay.merchant.controller;

import com.huimingpay.merchant.common.utils.SecurityUtil;
import com.huimingpay.transaction.api.PayChannelService;
import com.huimingpay.transaction.dto.PayChannelDTO;
import com.huimingpay.transaction.dto.PayChannelParamDTO;
import com.huimingpay.transaction.dto.PlatformChannelDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:PlatformParamController
 * Package:com.huimingpay.merchant.controller
 * Time: 2024/7/23
 */

@RestController
@Slf4j
@Api(value = "商户平台-渠道和支付参数相关", tags = "商户平台-渠道和支付参数")
public class PlatformParamController {
    @Reference
    PayChannelService payChannelService;

    @ApiOperation(value = "获取平台服务类型", notes = "请求交易服务查询平台支持的所有服务类型")
    @GetMapping(value = "/platform/services")
    public List<PlatformChannelDTO> getPlatformChannel() {
        return payChannelService.queryPlatformChannel();
    }

    @ApiOperation(value = "绑定平台服务类型", notes = "请求交易服务为指定应用添加服务类型")
    @ApiImplicitParams({@ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "platformChannelCodes", value = "平台服务类型", required = true, dataType = "String", paramType = "query")})
    @PostMapping(value = "/platform/bindForApp/{appId}")
    public String bindPlatformForApp(@PathVariable("appId") String appId,
                                     @RequestParam("platformChannelCodes") String platformChannelCodes) {
        payChannelService.bindPlatformChannelForApp(appId, platformChannelCodes);
        return "成功绑定";
    }


    @ApiOperation(value = "查询平台服务类型绑定状态", notes = "查询应用是否绑定了某个服务类型")
    @ApiImplicitParams({@ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "platformChannelCode", value = "平台服务类型", required = true, dataType = "String", paramType = "query")})
    @GetMapping(value = "/platform/bindStatus")
    public String queryPlatformChannelBindAppStatus(@RequestParam String appId, @RequestParam String platformChannelCode) {
        int status = payChannelService.queryAppBindPlatformChannel(appId, platformChannelCode);
        if (status == 1) {
            return "已绑定";
        }
        return "未绑定";
    }


    @ApiOperation(value = "查询服务类型对应支付渠道", notes = "根据平台服务类型获取支付渠道列表")
    @ApiImplicitParam(name = "platformChannelCode", value = "平台服务类型编码", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/platform/pay-channel/{platformChannelCode}")
    public List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable("platformChannelCode") String platformChannelCode) {
        return payChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
    }


    @ApiOperation(value = "配置支付渠道参数", notes = "商户配置支付渠道参数")
    @ApiImplicitParam(name ="payChannelParam", value ="商户配置支付渠道参数",
                     required =true, dataType ="PayChannelParamDTO", paramType ="body")
    @RequestMapping(value = "/platform/pay-channel-params", method = {RequestMethod.POST, RequestMethod.PUT})
    public String createPayChannelParam(@RequestBody PayChannelParamDTO payChannelParamDTO) {
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParamDTO.setMerchantId(merchantId);
        payChannelService.savePayChannelParam(payChannelParamDTO);
        return "支付渠道配置完毕";
    }


    @ApiOperation(value = "查询支付渠道参数列表", notes = "根据应用ID和服务类型编码获取支付渠道参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "platformChannelCode", value = "平台服务类型编码", required = true, dataType = "String", paramType = "path")})
    @GetMapping(value = "/platform/pay-channel-params/{appId}/{platformChannelCode}")
    public List<PayChannelParamDTO> queryPayChannelParams(@PathVariable("appId") String appId, @PathVariable("platformChannelCode") String platformChannelCode) {
        return payChannelService.queryPayChannelParamsByAppAndPlatformChannel(appId, platformChannelCode);
    }


    @ApiOperation(value = "查询支付渠道参数", notes = "根据应用ID和服务类型编码和支付渠道编码获取支付渠道参数")
    @ApiImplicitParams({@ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "platformChannelCode", value = "平台服务类型编码", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "payChannel", value = "支付渠道编码", required = true, dataType = "String", paramType = "path")})
    @GetMapping(value = "/platform/pay-channel-param/{appId}/{platformChannelCode}/{payChannel}")
    public PayChannelParamDTO queryPayChannelParam(@PathVariable String appId, @PathVariable String platformChannelCode,@PathVariable String payChannel) {
        return payChannelService.queryPayChannelParamByAppAndPlatformAndPayChannel(appId, platformChannelCode, payChannel);
    }

}
